//
//  TravelSearchViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelSearchViewController.h"
#import "DataAnalysis.h"
#import "TravelSearchCollectionViewCell.h"
#import "TravelyjANDlxdViewController.h"
#import "TravellvxingdiCollectionViewCell.h"

@interface TravelSearchViewController ()<UITextFieldDelegate,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UIView *titleView;

//用来存储国外数据
@property (nonatomic, retain) NSArray *foreignArray;
//用来存储国内数据
@property (nonatomic, retain) NSArray *domesticArray;

@property (nonatomic, retain) NSArray *changeArray;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UICollectionView *travelCollectionView;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UILabel *label;

@property (nonatomic, retain) UIButton *foreignBtn;

@property (nonatomic, retain) UIButton *domesticBtn;

@property (nonatomic, retain) UIButton *dele;
@end

@implementation TravelSearchViewController

- (void)dealloc
{
    [_textField release];
    [_titleView release];
    [_foreignArray release];
    [_domesticArray release];
    [_changeArray release];
    [_collectionView release];
    [_travelCollectionView release];
    [_tableView release];
    [_label release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initSearch];
    //初始化数组
    [self initArray];

    //初始化Button
    [self initButton];
    //初始化collectionView
    [self initCollectionView];
}
- (void)viewWillAppear:(BOOL)animated{
    
   
}
#pragma mark init初始化
// initSearch
- (void)initSearch{

        self.view.backgroundColor = [UIColor whiteColor];
        
        self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
        self.titleView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:self.titleView];
        [self.titleView release];
        [_titleView release];
        
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(10 * kWidth, 25 * kHeight, self.view.frame.size.width - 70 * kWidth, 30 * kHeight)];
        self.textField.borderStyle = UITextBorderStyleRoundedRect;
        self.textField.text = @"搜索:目的地, 游记..";
        self.textField.font = [UIFont systemFontOfSize:14 * kWidth];
        self.textField.textColor = [UIColor whiteColor];
        self.textField.backgroundColor = [[UIColor colorWithWhite:0.381 alpha:1.000]colorWithAlphaComponent:0.4];
        self.textField.layer.borderWidth = 1;
        self.textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.textField.layer.cornerRadius = 5 * kWidth;
    self.textField.delegate = self;
    self.textField.keyboardType = UIKeyboardTypeWebSearch;
        [self.titleView addSubview:self.textField];
        [_textField release];
        
        UIButton *cancle = [UIButton buttonWithType:UIButtonTypeCustom];
        cancle.frame = CGRectMake(self.textField.frame.size.width + kWidth * 20, 25 * kHeight, 40 * kWidth, 30 * kHeight);
        cancle.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
        [cancle setTitle:@"取消" forState:UIControlStateNormal];
        [cancle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.titleView addSubview:cancle];
        [cancle addTarget:self action:@selector(cancleAction:) forControlEvents:UIControlEventTouchUpInside];
}
//初始化button
- (void)initButton{

    UIView *viewLabel = [[UIView alloc] initWithFrame:CGRectMake(10 *kWidth, 70 *kHeight, 355 *kWidth, 25 *kHeight)];
    viewLabel.backgroundColor = [UIColor colorWithWhite:0.692 alpha:0.2];
    viewLabel.layer.cornerRadius = 4 *kWidth;
    [self.view addSubview:viewLabel];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(1 *kWidth, 1 *kHeight, 175.5 *kWidth, 23 *kHeight)];
    self.label.layer.cornerRadius = 4 *kWidth;
    self.label.layer.borderWidth = 1;
    self.label.layer.borderColor = [UIColor colorWithRed:0.882 green:0.882 blue:0.902 alpha:2].CGColor;
    self.label.backgroundColor = [UIColor whiteColor];
    [viewLabel addSubview:self.label];
    
    self.foreignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.foreignBtn.frame = CGRectMake(0, 0, 175.5 *kWidth, 25 *kHeight);
    self.foreignBtn.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.foreignBtn setTitle:@"国外" forState:UIControlStateNormal];
    [self.foreignBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.foreignBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [viewLabel addSubview:self.foreignBtn];
     [self change:self.foreignBtn];
    self.domesticBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.domesticBtn.frame = CGRectMake(175.5 *kWidth, 0, 175.5 *kWidth, 25 *kHeight);
    [self.domesticBtn setTitle:@"国内" forState:UIControlStateNormal];
    self.domesticBtn.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.domesticBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
    [self.domesticBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [viewLabel addSubview:self.domesticBtn];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 100 *kHeight, 375 *kWidth, 1*kHeight)];
    line.backgroundColor = [UIColor colorWithRed:0.882 green:0.882 blue:0.902 alpha:2];
    [self.view addSubview:line];
    
}
-(void)deleView
{
    self.dele = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.dele setTitle:@"x" forState:UIControlStateNormal];
    self.dele.frame = CGRectMake(self.textField.frame.size.width - 10 * kWidth, 30 * kHeight, 20 * kWidth, 20 * kHeight);
    [self.titleView addSubview:self.dele];
    [self.dele addTarget:self action:@selector(deleAction:) forControlEvents:UIControlEventTouchUpInside];
}
//初始化数组
- (void)initArray{
    
  self.foreignArray = @[@"卡塔尔国", @"日本", @"韩国", @"泰国", @"新加坡", @"马来西亚", @"越南", @"印度尼西亚", @"印度", @"土耳其", @"菲律宾", @"尼泊尔", @"柬埔寨", @"老挝", @"缅甸", @"文莱", @"蒙古国", @"斯里兰卡", @"以色列", @"阿联酋", @"伊朗", @"智利", @"巴西", @"阿根廷", @"巴巴多斯", @"美国", @"加拿大", @"墨西哥", @"古巴", @"南非", @"埃及", @"毛里求斯", @"摩洛哥", @"肯尼亚", @"奥地利", @"法国", @"英国", @"西班牙", @"意大利", @"德国", @"比利时", @"捷克", @"波兰", @"俄罗斯", @"葡萄牙", @"乌克兰", @"希腊", @"荷兰", @"芬兰", @"瑞士", @"摩纳哥公国", @"挪威", @"冰岛", @"澳大利亚", @"新西兰", @"大溪地", @"斐济", @"帕劳"];
    self.domesticArray = @[@"凤凰", @"厦门", @"威海", @"无锡", @"洛阳", @"澳门", @"西安", @"重庆", @"长沙", @"青岛", @"黄山", @"三亚", @"东莞", @"乌鲁木齐", @"岳山", @"沈阳", @"杭州", @"哈尔滨", @"桂林", @"南京", @"福州", @"贵阳", @"镇江", @"潮州", @"广州", @"北京", @"香港", @"台北", @"上海", @"深圳", @"武汉", @"伊犁", @"西塘", @"台中", @"银川", @"大理", @"满洲里", @"兰州", @"拉萨", @"垦丁", @"成都", @"大连", @"开封", @"敦煌", @"苏州", @"平遥", @"海口", @"九寨沟", @"南昌", @"张掖", @"烟台", @"北海", @"日照", @"天津", @"绍兴", @"济南", @"扬州", @"珠海", @"乌镇", @"西宁", @"太原", @"丽江", @"宏村", @"承德", @"林芝", @"泰山", @"稻城", @"盘锦", @"连云港", @"都江堰", @"腾冲", @"南宁", @"郑州", @"泉州", @"天水", @"丰宁坝上", @"武夷山", @"甘南", @"九份", @"宁波", @"西昌", @"高雄", @"张家界", @"周庄", @"漠河", @"长春", @"千岛湖", @"丹东", @"舟山", @"西双版纳", @"雪乡", @"遵义", @"霞浦", @"三清山", @"北戴河", @"宣城", @"额济纳旗", @"佛山", @"束河古镇", @"黄果树", @"婺源", @"花莲", @"海螺沟", @"石家庄", @"呼伦贝尔"];
    
    self.changeArray = [NSMutableArray array];
}
- (void)initCollectionView{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake(115 * kWidth - (1 - kWidth) * 10 , 25 *kHeight);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100 *kWidth , self.view.frame.size.width , 567 * kHeight - (1 - kWidth) * 20) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.collectionView setBackgroundColor:[UIColor colorWithRed:0.922 green:0.941 blue:0.961 alpha:1]];
    
    [self.collectionView registerClass:[TravelSearchCollectionViewCell class]forCellWithReuseIdentifier:@"TravelSearchCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [layout release];


}

- (void)initMainCollectionView{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize = CGSizeMake(375 *kWidth , 567 *kHeight);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100 *kWidth , self.view.frame.size.width , 567 *kHeight) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.collectionView setBackgroundColor:[UIColor colorWithRed:0.922 green:0.941 blue:0.961 alpha:1]];

    

    [self.collectionView registerClass:[TravellvxingdiCollectionViewCell class] forCellWithReuseIdentifier:@"TravellvxingdiCollectionViewCell"];

    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[TravelSearchCollectionViewCell class] forCellWithReuseIdentifier:@"TravelSearchCollectionViewCell"];
    
    [_collectionView release];
    
    [layout release];
    


}


#pragma mark action
- (void)cancleAction:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)change:(UIButton *)btn{

    if (btn == self.domesticBtn) {
        NSLog(@"国内");
        [self.foreignBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
         [self.domesticBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(177.5 * kWidth, 1 * kHeight, 175.5 * kWidth, 23 * kHeight);
        }];
        self.changeArray = self.domesticArray;
        [self.collectionView reloadData];
        
    }else{
        NSLog(@"国外");
        [self.foreignBtn setTitleColor:[UIColor orangeColor]  forState:UIControlStateNormal];
        [self.domesticBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8]forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(1 * kWidth, 1 * kHeight, 176.5 * kWidth, 23 * kHeight);
        }];
        self.changeArray = self.foreignArray;
        
        [self.collectionView reloadData];
    }
}
- (void)deleAction:(UIButton *)btn{

    self.textField.text = nil;
    [self.textField resignFirstResponder];
}
#pragma mark textField 代理回调方法
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    self.textField.text = nil;
    [textField becomeFirstResponder];
    //调用dele
    [self deleView];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    TravelyjANDlxdViewController *and = [[TravelyjANDlxdViewController alloc] init];
    and.name = self.textField.text;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:and animated:YES];
    [self.textField resignFirstResponder];
    return YES;
    
}
#pragma mark collectionView代理回调方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.changeArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        TravelSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelSearchCollectionViewCell" forIndexPath:indexPath];
    cell.name = self.changeArray[indexPath.row];
    return cell;

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(5 *kHeight, 5 *kWidth, 5 *kWidth, 5*kHeight);

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self.textField becomeFirstResponder];
    self.textField.text = self.changeArray[indexPath.row];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
